package com.leyou.item.mapper;

import org.apache.commons.lang.StringUtils;

/**
 * @Author yong.Mr
 * @data 2019-8-9
 * @version 1.0.0
 */
public class BrandMapperProvider {

    /**
     * 为BrandMapper中queryBrandByPageSqls方法生成相应的动态SQL语句
     */
    public String getSqlQueryBrandByPage(String key, Integer page, Integer rows,
                                         String sortBy, Boolean desc, Boolean flag){
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT * FROM tb_brand");
        if(rows == -1) {
            return sql.toString();
        }
        sql.append(" WHERE `name` LIKE '%" + key + "%'" + " OR letter = '" + key +  "'");
        if (StringUtils.isNotBlank(sortBy)) {
            sql.append(" ORDER BY ");
            sql.append(sortBy + " " + (desc ? "desc" : "asc"));
        }
        // true代表查询分页数据, false代表查询所有数据
        if(flag) {
            sql.append(" LIMIT ");
            sql.append(((page - 1) * rows) + "," + rows);
        }
        System.out.println("拼接之后的字符串语句----->:" + sql.toString());
        return sql.toString();
    }
}
